﻿@{
    ViewBag.Title = "合班";
    Layout = "~/Views/Shared/___newestMaster.cshtml";
}

@using ClassModel
@using ClassCommon
@using ClassBLL

@{
    // 课程列表
    List<t_class_course> coursesList = ViewBag.courseList;  
}

@section style {
    <style type="text/css">
        p {
            cursor: pointer;
        }
    </style>

}

<div class="gztj">
    <div class="tisp">
        <div class="conConTo1 col-lg-12 col-md-12 col-sm-12">
            <ul>
                <li>贴心tips</li>
                <li>
                    <p>注意</p>
                    <p>请设置学校的教学时间、年级/班级固定无课时间、课程/教师无固定时间以及连课堂和合班课</p>
                </li>
            </ul>
        </div>
        <div class="col-lg-12 col-md-12 col-sm-12" style="float: left; padding: 0;">
            <div class="pkhb col-lg-6 col-md-6 col-sm-12">
                <ul>
                    <li class="col-lg-6 col-md-6 col-sm-6 noSchedule" style="overflow: hidden">不排课时间</li>
                    <li class="col-lg-6 col-md-6 col-sm-6 hb pkhbbg">合班</li>
                </ul>
            </div>
        </div>

        <div class="yeshb">
            <div style="float: left;" class="col-lg-12 col-md-12 col-sm-12">
                <div class="boxL col-lg-6 col-md-12 col-sm-12">
                    <div class="box1 col-lg-6 col-md-12 col-sm-6">
                        <div class="box1top col-lg-10 col-md-12 col-sm-12">
                            <p>选择课程</p>
                        </div>
                        <div class="box1botton col-lg-10 col-md-12 col-sm-12">
                            <ul class="course">
                                @if (coursesList != null)
                                { 
                                    <!-- 第一步，课程选择循环HTML (这个是单选) --> 
                                    // <a href="javascript:void(0)" class="list-group-item" data-courseid="@(coursesList[c].id)">@(coursesList[c].coursename)</a>
                                    for (int c = 0; c < coursesList.Count; c++)
                                    {
                                    <li data-courseid="@(coursesList[c].id)">@(coursesList[c].coursename)</li>
                                    }
                                }
                            </ul>
                        </div>
                    </div>
                    <div class="box2 col-lg-6 col-md-12 col-sm-6">
                        <div class="box2top col-lg-12 col-md-12 col-sm-12">
                            <p>选择班级</p>
                        </div>
                        <div class="box2botton col-lg-12 col-md-12 col-sm-12">
                            <p>合理必选选择相同的老师，否则会导致排课失败。</p>
                        </div>
                        <div class="box2bt">
                            <ul class="classes" id="dxns-div-class">
                                <!-- action5 -->
                                @*<li class="actions5">高二1班</li>*@
                                <!-- 老师和班级HTML -->

                            </ul>
                        </div>
                    </div>
                </div>

                <div class="boxR col-lg-6 col-md-12 col-sm-12">
                    <div class="boxR2 col-lg-4 col-md-12 col-sm-12">
                        <p id="btn-merge">合并</p>
                        <p id="dxns-btn-remove">取消合并</p>
                    </div>
                    <div class="boxR3 col-lg-8 col-md-12 col-sm-12">
                        <div class="boxR3T">
                            <p>已存在合班级</p>
                        </div>
                        <div class="boxR3B">
                            <ul id="dxns-div-mergedclass">
                                @* <li class="actions7">数学：宁文瑞丶李子杰(1班丶2班)</li>*@
                                <!-- 合班数据容器 -->
                            </ul>
                        </div>
                    </div>
                </div>

            </div>
            <div class='hbbt col-lg-3 col-md-12 col-sm-2 beginPK'>
                合并完毕，开始排课
            </div>
        </div>
    </div>
</div>

<!-- 当前任务id -->
<input type="hidden" id="taskid" value="@(ViewBag.RawTaskId)"/>
<input type="hidden" id="fileid" value="@(ViewBag.fileid)"/>

@section script {
    <script type="text/javascript">
        // 不排课跳转链接
        $(".noSchedule").on("click", function () {
            window.location.href = "ruleIndex?taskid=" + $("#taskid").val();
        })

        // 开始排课任务链接
        $(".beginPK").on("click", function () {
            window.location.href = "/ScheduleClass/BeginSchedule?taskid=" + taskid;
        })
        
        // 文件id
        var fileid = $("#fileid").val();
        // 任务id
        var taskid = $("#taskid").val();

        /// 需要插入和班级相关老师和课程数据
        var $teacherClassContainer = $("#dxns-div-class");
        /// 合班容器
        var $mergeClassContainer = $("#dxns-div-mergedclass");

        /// 预加载合班数据
        (function () {
            $.get("GetMergeInfoWithTaskId", { taskid: taskid }, function (data) {
                if (data.mergeList.length > 0) {
                    // 一条就是一个合班数据
                    data.mergeList.forEach(function (merge, index) {
                        var classids = merge.classIds; //课程的id
                        var teacherids = merge.teacherIds; // 老师的id
                        var classCount = merge.classCount;
                        //老师名字、班级名字
                        // ajax 同步请求
                        var teacherNames;
                        $.ajax({
                            url: "GetTeachersInfoWithTeacherIds",
                            data: { teacherIds: teacherids },
                            type: "GET",
                            async: false,
                            dataType: "json",
                            success: function (data) {
                                teacherNames = data.result;
                            }
                        });

                        var classNames;
                        $.ajax({
                            url: "GetClassInfoWithClassIds",
                            data: { classIds: classids },
                            type: "GET",
                            async: false,
                            dataType: "json",
                            success: function (data) {
                                classNames = data.result;
                            }
                        });

                        var mergeHTML2 = "<li href='javascript:void(0)' data-id='" + merge.id + "' data-courseid='" + merge.courseid + "' data-classcount='" + classCount + "' data-classids='" + classids + "'>" + merge.courseName + "：" + teacherNames + "(" + classNames + ")</li>";

                        $mergeClassContainer.append(mergeHTML2);

                        // 绑定事件
                        splitMerge();
                    });
                }
            })
        })();

        /// 分割合班
        function splitMerge() {
            $("#dxns-div-mergedclass li").unbind("click");
            // 拆分合班
            $("#dxns-div-mergedclass li").on("click", function () {
                var $this = $(this);
                $this.toggleClass("actions7");
                $this.siblings().removeClass("actions7");
            });
        }


        // 拆分合班
        $("#dxns-btn-remove").on("click", function () {
            if (!$("#dxns-div-mergedclass li").hasClass("actions7")) {
                showFailure("请选择一个需要拆分的合班选项!");
                // alert("请选择一个需要拆分的合班选项!");
                return;
            }

            $("#dxns-div-mergedclass li").each(function (index, item) {
                var $item = $(item);
                if ($item.hasClass("actions7")) {
                    $.get("DeleteMergeInfo", { id: $(item).attr("data-id"), taskid: taskid }, function (data) {
                        if (data.status) {
                            $item.remove();
                        }
                    })
                }
            });
        });

        // 点击合班按钮
        $("#btn-merge").on("click", function () {
            var itemList = [];
            $teacherClassContainer.find("li").each(function (index, item) {
                var $item = $(item);
                if ($item.hasClass("actions5")) {
                    itemList.push($item);
                    //$mergeClassContainer.append($item);
                    //$teacherCourseContainer.remove($item);
                    $item.removeClass("action5");
                }
            })

            if (itemList.length == 0) { showFailure("请选择需要合班的课程和班级!"); return; }

            // 班级个数
            var classCount = itemList.length;
            // 课程名称
            var courseName = $(itemList[0]).attr("data-coursename");
            // 课程id
            var courseid = $(itemList[0]).attr("data-courseid");
            // 合班的老师姓名
            var teacherNames = "";
            var teacherids = ""; // 合班的老师id
            // 合班的班级姓名
            var classNames = "";
            var classids = ""; // 合班的班级id

            itemList.forEach(function (item, index) {
                teacherNames += $(item).attr("data-teachername") + ",";
                teacherids += $(item).attr("data-teacherid") + ",";
                classNames += $(item).attr("data-classname") + ",";
                classids += $(item).attr("data-classid") + ",";
            });

            // 格式整理
            teacherNames = teacherNames.substring(0, teacherNames.length - 1);
            teacherids = teacherids.substring(0, teacherids.length - 1);
            classNames = classNames.substring(0, classNames.length - 1);
            classids = classids.substring(0, classids.length - 1);

            /*
            <a href="javascript:void(0)" class="list-group-item" data-courseid="5" data-classcount="2" data-classids="3,4">劳技：苟明远,苟明远(3班,4班)</a>
            */

            // 合班的HTML拼接
            var mergeHTML = " <li href='javascript:void(0)' data-courseid='" + courseid + "' data-classCount='" + classCount + "' data-classids='" + classids + "'>" + courseName + "：" + teacherNames + "(" + classNames + ")</li>";
            $mergeClassContainer.append(mergeHTML); // 将凭借好的HTML插入合班容器。

            itemList.forEach(function (item, index) {
                console.log(typeof item + ' index :' + index);
                $(item).remove(); // 直接用元素的remove方法删除自己即可
            });

            // 绑定事件
            splitMerge();

            // 需要把合班信息存入到数据库 ajax请求 (fileid,or taskid)
            /* courseid(课程id) classCount(合班的班级个数) 1，2，3 分别是班级编号  */

            $.post("SaveMergeInfo", {
                courseid: courseid,
                classCount: classCount,
                classids: classids,
                taskid: taskid,
                teacherids: teacherids
            },
            function (data) {
                if (data.status) {
                    $mergeClassContainer.find("a:last").attr("data-id", data.status); // 绑定id信息

                    showSuccess("合班信息保存成功!");
                } else {
                    showFailure("合班信息已经存在，无需重复保存!");
                }
            });
        })

        // 课程单选
        $("ul.course li").on("click", function () {
            var $this = $(this);
            $this.addClass("actions6");
            $this.siblings().removeClass("actions6");

            // 根据当前的courseid，获取老师信息
            $.get("GetTeacherClassInfo", {
                courseid: $this.attr("data-courseid"),
                fileid: fileid
            }, function (data) {
                if (data.status) {
                    /*<a class="list-group-item" classid="2645732200890369" teacherid="2645732200890385" groupid="0">高二1班(裔春蕾)</a>*/
                    var html = "";
                    for (var i = 0; i < data.list.length; i++) {
                        html += "<li data-classid='"
                            + data.list[i].classid + "' data-teacherid='"
                            + data.list[i].teacherid + "' data-courseid='"
                            + data.list[i].courseid + "' data-coursename='"
                            + data.list[i].courseName + "' data-classname='"
                            + data.list[i].className + "' data-grade='"
                            + data.grade + data.list[i].className + "' data-teacherName='"
                            + data.list[i].teacherName + "'>"
                            + data.grade + ""
                            + data.list[i].className
                            + "(" + data.list[i].teacherName + ")</li>";
                    }

                    $teacherClassContainer.html(html);

                    // 绑定单击事件
                    bindTC();
                } else {
                    alert("数据请求失败!");
                }
            });
        })

        // 默认加载第一个课程的老师班级数据
        setTimeout(function () {
            $(".course li").eq(0).click();
        }, 0);

        // 老师、班级可以多选
        function bindTC() {
            $("ul.classes li").on("click", function () {
                var $this = $(this);
                $this.toggleClass("actions5");

            })
        }
    </script>
}

